package com.byq.mm.application.cluster.client;
import com.byq.mm.abstractx.device.Device;
import com.byq.mm.abstractx.engine.CoreEnvironment;
import com.byq.mm.abstractx.protocol.Interaction;
import com.byq.mm.application.cluster.provider.ObdProvider;
import com.byq.mm.application.cluster.provider.PlayerProvider;
import com.byq.mm.foundation.cluster.client.BasicClusterClient;
import com.byq.mm.foundation.cluster.datatype.AttributeValue;
import com.byq.mm.foundation.cluster.datatype.BooleanValue;
import com.byq.mm.foundation.cluster.datatype.IntValue;
import com.byq.mm.foundation.cluster.datatype.StringListValue;

import java.util.List;

public class PlayerClient extends BasicClusterClient {

	public PlayerClient(String cluster, Device device, CoreEnvironment e) {
		super(cluster, device, e);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected Class<? extends AttributeValue> getAttributeType(String attributeName) {
		
		return PlayerProvider.Define.attributeTypes.get(attributeName);
	}

	@Override
	protected void init() {
		attributes = PlayerProvider.Define.init();
		
	}
	public List<String> getList(){
		StringListValue v = (StringListValue)this.getAttributeValue(PlayerProvider.Define.Attributes.PLAYLIST);
		return v.getValue();
	}
	public int getStatus(){
		IntValue v = (IntValue)this.getAttributeValue(PlayerProvider.Define.Attributes.STATUS);
		return v.getValue();
	}
	public int getCurrentIndex(){
		IntValue v = (IntValue)this.getAttributeValue(PlayerProvider.Define.Attributes.CURRENT_INDEX);
		return v.getValue();
	}
	public int getVolume(){
		IntValue v = (IntValue)this.getAttributeValue(PlayerProvider.Define.Attributes.VOLUME);
		return v.getValue();
	}
	public int getProgress(){
		IntValue v = (IntValue)this.getAttributeValue(PlayerProvider.Define.Attributes.PROGRESS);
		return v.getValue();
	}
	public int getCurrentLength(){
		IntValue v = (IntValue)this.getAttributeValue(PlayerProvider.Define.Attributes.CURRENT_LENGTH);
		return v.getValue();
	}
	public void select(int index){
		this.writeAttribute(PlayerProvider.Define.Attributes.CURRENT_INDEX, new IntValue(index));
	}
	public void play(){
		this.writeAttribute(PlayerProvider.Define.Attributes.STATUS, new IntValue(1));
	}
	public void pause(){
		this.writeAttribute(PlayerProvider.Define.Attributes.STATUS, new IntValue(3));
	}
	public void stop(){
		this.writeAttribute(PlayerProvider.Define.Attributes.STATUS, new IntValue(0));
	}
	public void progress(int p){
		this.writeAttribute(PlayerProvider.Define.Attributes.PROGRESS, new IntValue(p));
	}
	public void volume(int p){
		this.writeAttribute(PlayerProvider.Define.Attributes.VOLUME, new IntValue(p));
	}
	public void toggle(){
		BooleanValue v = (BooleanValue)this.getAttributeValue(PlayerProvider.Define.Attributes.ACTIVE);
		this.writeAttribute(PlayerProvider.Define.Attributes.ACTIVE, new BooleanValue(!v.isValue()));
	}
	public boolean isActive(){
		BooleanValue v = (BooleanValue)this.getAttributeValue(PlayerProvider.Define.Attributes.ACTIVE);
		return v.isValue();
	}
	public void toggleMute(){
		command(PlayerProvider.Define.ClientCommand.MUTE);
	}
	public void refresh(){
		this.readAttribute(PlayerProvider.Define.Attributes.ACTIVE);
		this.readAttribute(PlayerProvider.Define.Attributes.STATUS);
		this.readAttribute(PlayerProvider.Define.Attributes.PLAYLIST);
		this.readAttribute(PlayerProvider.Define.Attributes.PROGRESS);
		this.readAttribute(PlayerProvider.Define.Attributes.CURRENT_INDEX);
		this.readAttribute(PlayerProvider.Define.Attributes.CURRENT_LENGTH);
	}
}
